package com.leo.utils;

import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;

/**
 * px与dip转换公式
 * http://jinganglang777.blog.163.com/blog/static/790854972011911111537418/ pixs
 * =dips * (densityDpi/160). dips=(pixs*160)/densityDpi
 *
 * created by leo
 */
public class DipUtils {

    private DipUtils() {}

    public static int dip2px(float dipValue) {
        return (int) dip2pxF(dipValue);
    }

    public static int px2dip(float pxValue) {
        return (int) px2dipF(pxValue);
    }

    public static float dip2pxF(float dipValue) {
        DisplayMetrics r = Resources.getSystem().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, r);
    }

    public static float px2dipF(float pxValue) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        return pxValue / (metrics.densityDpi / 160f);
    }
}
